/**
 * 时间日期工具类
 */

export default {
	// 获取指定天数前或后的日期
	getDate(n, date) {
		date = date || new Date();
		let timestamp = date.getTime();
		let newDate = new Date(timestamp + n * 24 * 3600 * 1000);
		let year = newDate.getFullYear();
		let month = newDate.getMonth() + 1;
		month = month.toString().length === 1 ? "0" + month : month;
		let day =
			newDate.getDate().toString().length === 1 ?
			"0" + newDate.getDate() :
			newDate.getDate();
		const time = [year, month, day].join("-");
		return time;
	},
	// 获取今天日期
	getToday() {
		return this.getDate(0);
	},
	// 获取昨天日期
	getYesterday() {
		return this.getDate(-1);
	},
	// 获取明天日期
	getTomorrow() {
		return this.getDate(1);
	},
	// 获取前七天日期范围
	getLast7Days() {
		const startDate = this.getDate(-6);
		const endDate = this.getToday();
		return {
			startDate,
			endDate
		};
	},
	// 获取后七天日期范围
	getNext7Days() {
		const startDate = this.getTomorrow();
		const endDate = this.getDate(6, new Date(startDate));
		return {
			startDate,
			endDate
		};
	},
	// 获取本周日期范围
	getCurrentWeek() {
		let now = new Date();
		let firstDayOfWeek = new Date(
			now.setDate(now.getDate() - now.getDay() + 1)
		);
		let lastDayOfWeek = new Date(now.setDate(now.getDate() - now.getDay() + 7));
		const startDate = this.getDate(0, firstDayOfWeek);
		const endDate = this.getDate(0, lastDayOfWeek);
		return {
			startDate,
			endDate
		};
	},
	// 获取本月日期范围
	getCurrentMonth() {
		let myDate = new Date();
		let tYear = myDate.getFullYear();
		let tMonth = myDate.getMonth();
		tMonth = this.doHandleZero(tMonth + 1);
		const startDate = tYear + "-" + tMonth + "-01";
		// const endDate = tYear + '-' + tMonth + '-' + new Date(tYear, tMonth, 0).getDate()
		const endDate = this.getDate(0);
		return {
			startDate,
			endDate
		};
	},
	// 补零函数
	doHandleZero(zero) {
		let date = zero;
		if (zero.toString().length === 1) {
			date = "0" + zero;
		}
		return date;
	}
};

/**
 * 时间戳 转  YYYY-MM-DD hh:mm:ss
 * @param timestamp
 * @returns {string}
 * @Author  Chris Lee
 */
export function formatDate(timestamp) {
	let date = new Date(timestamp);
	let Y = date.getFullYear() + "-";
	let M =
		(date.getMonth() + 1 < 10 ?
			"0" + (date.getMonth() + 1) :
			date.getMonth() + 1) + "-";
	let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
	let h =
		(date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
	let m =
		(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
		":";
	let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	return Y + M + D + h + m + s;
}


// // 获取前一天
// export const getPreviousDay = (dateStr) => {
// 	// 提取分隔符（支持 - / . 等非数字字符）
// 	const separator = dateStr.match(/[^0-9]/)?.[0] || '-';
// 	// 分割年月日
// 	const [yearStr, monthStr, dayStr] = dateStr.split(separator);
// 	const year = parseInt(yearStr, 10);
// 	const month = parseInt(monthStr, 10) - 1; // 月份从 0 开始
// 	const day = parseInt(dayStr, 10);

// 	// 创建日期对象并减去一天
// 	const date = new Date(year, month, day);
// 	date.setDate(date.getDate() - 1);

// 	// 格式化为字符串（补零并保持原分隔符）
// 	const y = date.getFullYear();
// 	const m = String(date.getMonth() + 1).padStart(2, '0');
// 	const d = String(date.getDate()).padStart(2, '0');

// 	return [y, m, d].join(separator);
// }
// // 后一天
// export const getNextDayOrNull = (dateStr) => {
// 	// 1. 提取分隔符和日期部分
// 	const separator = dateStr.match(/[^0-9]/)?.[0] || '-';
// 	const [yearStr, monthStr, dayStr] = dateStr.split(separator);

// 	// 2. 转换为日期对象（注意月份从0开始）
// 	const inputDate = new Date(
// 		parseInt(yearStr),
// 		parseInt(monthStr) - 1,
// 		parseInt(dayStr)
// 	);

// 	// 3. 检查日期有效性
// 	if (isNaN(inputDate.getTime())) return null;

// 	// 4. 检查是否是今天
// 	const today = new Date();
// 	today.setHours(0, 0, 0, 0); // 清除时间部分
// 	inputDate.setHours(0, 0, 0, 0);
// 	if (inputDate.getTime() === today.getTime()) return null;

// 	// 5. 计算后一天
// 	const nextDate = new Date(inputDate);
// 	nextDate.setDate(nextDate.getDate() + 1);

// 	// 6. 格式化输出（保持原分隔符）
// 	const y = nextDate.getFullYear();
// 	const m = String(nextDate.getMonth() + 1).padStart(2, '0');
// 	const d = String(nextDate.getDate()).padStart(2, '0');

// 	return [y, m, d].join(separator);
// }

export const getPreviousDay = (dateStr) => {
	const separator = dateStr.match(/[^0-9]/)?.[0] || '-';
	const [yearStr, monthStr, dayStr] = dateStr.split(separator);
	const year = parseInt(yearStr, 10);
	const month = parseInt(monthStr, 10) - 1;
	const day = parseInt(dayStr, 10);

	const date = new Date(year, month, day);

	// 防止减到 0000 年前，JS 的最小支持为约 100 年前
	if (isNaN(date.getTime()) || year <= 1) return null;

	date.setDate(date.getDate() - 1);

	// 再次检查防止越界
	if (date.getFullYear() < 1) return null;

	const y = date.getFullYear();
	const m = String(date.getMonth() + 1).padStart(2, '0');
	const d = String(date.getDate()).padStart(2, '0');

	return [y, m, d].join(separator);
};
export const getNextDayOrNull = (dateStr) => {
	const separator = dateStr.match(/[^0-9]/)?.[0] || '-';
	const [yearStr, monthStr, dayStr] = dateStr.split(separator);

	const year = parseInt(yearStr, 10);
	const month = parseInt(monthStr, 10) - 1;
	const day = parseInt(dayStr, 10);

	const inputDate = new Date(year, month, day);
	if (
		isNaN(inputDate.getTime()) ||
		year < 1 || year >= 9999 // 防止 Date(9999, 11, 31) + 1 越界
	) return null;

	const today = new Date();
	today.setHours(0, 0, 0, 0);
	inputDate.setHours(0, 0, 0, 0);

	if (inputDate.getTime() === today.getTime()) return null;

	const nextDate = new Date(inputDate);
	nextDate.setDate(nextDate.getDate() + 1);

	if (nextDate.getFullYear() > 9999) return null;

	const y = nextDate.getFullYear();
	const m = String(nextDate.getMonth() + 1).padStart(2, '0');
	const d = String(nextDate.getDate()).padStart(2, '0');

	return [y, m, d].join(separator);
};
export const getFormattedDate = (date = new Date(), separator = '-') => {
	const y = date.getFullYear();
	const m = String(date.getMonth() + 1).padStart(2, '0');
	const d = String(date.getDate()).padStart(2, '0');
	return [y, m, d].join(separator);
};